package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.User;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 人物信息表dao
 * @author Administrator
 *
 */
public class UserDaoImp implements CommnonDao<User>{
	/**
	 * 保存内容
	 * @param value
	 * @return 
	 * @throws Exception
	 */
	@Override
	public long save(User value) throws Exception {
		//判断类   instanceof 
		User user = (User)value;
		String sql="INSERT INTO t_user(user_account,user_name,password,create_time,user_type) VALUES(?,?,?,?,?)";
		int num=(int)JdbcUtil.insertAuto(sql, user.getUserAccount(),user.getUserName(),user.getPassword(),user.getCreateTime(),user.getUserType());
		return num;
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(User value) throws Exception {
		String sql="UPDATE t_user SET user_name=?,password=?,user_type =?,user_state=? WHERE user_account=? OR user_id = ?;";
		int num=JdbcUtil.update(sql,
				value.getUserName(),
				value.getPassword(),
				value.getUserType(),
				value.getUserState(),
				value.getUserAccount(),
				value.getUserId());
		return num;
	}
	/**
	 * 删除内容 
	 * 不能删，只能将状态改为冻结
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(User value) throws Exception {
		return 0;	
	}
	/**
	 * 查找所有的内容
	 * 人物角色表  t_character_role
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll(int pageNo, int pageSize) throws Exception {
		String sql="select * from t_user limit ?,?";
		return (List<User>)JdbcUtil.query(User.class, sql, (pageNo-1)*pageSize,pageSize);
	}
	
	/**
	 * 查找所有内容
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<User> findAll() throws Exception {
		String sql="select * from t_user ";
		return (List<User>)JdbcUtil.query(User.class, sql);
	}
	/**
	 * 查找单一实体
	 * 人物角色表  t_character_role
	 * @param Key
	 * @throws Exception
	 */
	@Override
	public User findByKey(Object key) throws Exception {
		//user_account 不能为纯数字
		return (User) JdbcUtil.uniqueBean(User.class,
			"select * from t_user where user_id = ? or user_account = ?",
				key, key);
	}
	/**
	 * 用于导出excel
	 * @return
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<User> findAll(Object key, int pageNo, int pageSize) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
}
